﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.CommonHelpers;
using WHControlLib.Controls;
using WHControlLib.Forms;

namespace WHUIdemo.Forms.PosDemoPageFrm
{
    
    public delegate void CheckOutOverClear();
    public delegate void del_GetLastOrderInfo(double LastRealPayMoney, double LastRecevialblePayMoney, double LastCheckOutMoney);
    public partial class GoodsCheckOutFrm : WHDialogFrm
    {
       public CheckOutOverClear checkOutOverClear;
        public del_GetLastOrderInfo Del_getlastOrderInfo;


        GoodsCheckOutInfo InCheckOutInfo;

        double alReadPayMoney = 0.00, waitPayMoney = 0.00, GoBackCustomMoney = 0.00;

        double RealNeedPayMonty = 0.00;

        public GoodsCheckOutFrm(GoodsCheckOutInfo CheckOutInfo)
        {
            InitializeComponent();
            InCheckOutInfo = CheckOutInfo;



        }

        private void GoodsCheckOutFrm_Paint(object sender, PaintEventArgs e)
        {
            
        }

      

        private void GoodsCheckOutFrm_Load(object sender, EventArgs e)
        {
            whButton1.IsSelectedMy = true;
            waitPayMoney = InCheckOutInfo.RealPayMoney;
            RealNeedPayMonty=InCheckOutInfo.RealPayMoney;

            RealMoneyLB.Text =  "￥" + InCheckOutInfo.RealPayMoney.ToString("F2");
            ReceivaleMoneyLB.Text = "￥" + InCheckOutInfo.ReceivableMoney.ToString("F2");


            DisplayWaitAndAlreadyMoney(alReadPayMoney,waitPayMoney);

        }
       void DisplayWaitAndAlreadyMoney(double alreadymoney,double waitPayMoney)
        {
            this.Invoke(new Action(() => {

                WaitPayMoneyLB.Text = "待收" + '\n' + '\n' + "￥" + waitPayMoney.ToString("F2");
                AlreadyPayMoneyLB.Text = "已付" + '\n' + '\n' + "￥" + alreadymoney.ToString("F2");

                WaitPayMoneyLB.Invalidate();
                AlreadyPayMoneyLB.Invalidate();


            }));




       


        }

        void SetPayClasSCtrl(object sender)
        {
            if (!((WHButton)sender is WHButton))
            {
                return;
            }
            WHButton wb = (WHButton)sender;
            PayNameLB.Text = wb.Text;
            wb.IsSelectedMy = true;
            foreach (var item in wb.Parent.Controls)
            {
                if (item is WHButton)
                {
                    WHButton w = (WHButton)item;
                    if (w != wb)
                    {
                        w.IsSelectedMy = false;
                    }

                }
            }

        }

        private void whButton1_Click(object sender, EventArgs e)
        {

            SetPayClasSCtrl(sender);



        }

        private void whButton2_Click(object sender, EventArgs e)
        {
            SetPayClasSCtrl(sender);
        }

        private void whButton4_Click(object sender, EventArgs e)
        {
            SetPayClasSCtrl(sender);
        }

        private void whButton3_Click(object sender, EventArgs e)
        {
            SetPayClasSCtrl(sender);
        }

        private void GoodsCheckOutFrm_Activated(object sender, EventArgs e)
        {
            PayBox.Focus();
           
        }

        private void whNumKeyBoard2_OKButtonCLick(object sender, EventArgs e)
        {
            double PayManey = 0;
            GoBackCustomMoney = 0;
            if (DataCheckHelper.IsNotfufolat(PayBox.Text.Trim()) && double.TryParse(PayBox.Text.Trim(), out PayManey))
            {
             double NeedPayMoney = PayManey-waitPayMoney  ;
               
                   
                if (NeedPayMoney < 0)
                {
                    waitPayMoney = waitPayMoney - PayManey;
                    alReadPayMoney += PayManey;

                    PayBox.Text = "";
                    PayBox.Focus();
                    
                    DisplayWaitAndAlreadyMoney(alReadPayMoney, waitPayMoney);

                }
                else
                {

                  //找回的钱
                    GoBackCustomMoney = alReadPayMoney + PayManey - RealNeedPayMonty;
                    if (GoBackCustomMoney>100)
                    {
                      
                         WHMsgDialog.ShowMsgDialog("找回的金额不能大于100 ！");
                        PayBox.Focus();
                       
                        return;
                    }
                    Del_getlastOrderInfo( alReadPayMoney + PayManey,InCheckOutInfo.RealPayMoney, GoBackCustomMoney);



                    alReadPayMoney = 0;
                    waitPayMoney = 0;
                    checkOutOverClear();
                    DoCheckOutOverSomeThing();
                    this.Close();

                }
            }
        }
        void DoCheckOutOverSomeThing()
        { }
        private void PayBox_TextChanged(object sender, EventArgs e)
        {
           
            double nowPayMoney=0;
         

            if (DataCheckHelper.IsNotfufolat(PayBox.Text.Trim())&&double.TryParse(PayBox.Text.Trim(),out nowPayMoney))
            {
               double  RemainMoney=  waitPayMoney - nowPayMoney;
                if (RemainMoney < 0)
                {

                    FindPayBox.Text = (0-RemainMoney).ToString("F2");
                 
                   

                }
                else FindPayBox.Text = "0.00";
                 FindPayBox.Invalidate();

            }
             
        }
    }
}
